<?php

namespace plugin\enterprise\app;

use plugin\enterprise\app\model\EnterpriseMenu;
use plugin\enterprise\app\model\EnterprisePermission;
use plugin\enterprise\app\model\EnterpriseRole;
use plugin\enterprise\app\model\EnterpriseUser;
use plugin\enterprise\app\service\EnterpriseSettingService;
use plugin\owladmin\app\support\Cores\Menu;
use Shopwwi\WebmanAuth\Facade\Auth;
use support\Container;

class Enterprise extends \plugin\owladmin\app\Admin
{

    public static function view($apiPrefix = ''): array|string|null
    {
        $view = file_get_contents(public_path('admin-assets/index.html'));

        $script = '<script>window.$adminApiPrefix = "/enterprise-api"</script>';

        return preg_replace('/<script>window.*?<\/script>/is', $script, $view);
    }

    /**
     * @return Menu;
     */
    public static function menu(): Menu
    {
        return new \plugin\enterprise\app\support\Cores\Menu();
    }

    public static function setting(): EnterpriseSettingService
    {
        return EnterpriseSettingService::make();
    }

    public static function enterpriseMenuModel(): string
    {
        return self::config('enterprise.models.enterprise_menu', EnterpriseMenu::class);
    }

    public static function EnterpriseUserModel(): string
    {
        return self::config('enterprise.models.enterprise_user', EnterpriseUser::class);
    }

    public static function enterprisePermissionModel(): string
    {
        return self::config('enterprise.models.enterprise_permission', EnterprisePermission::class);
    }

    public static function enterpriseRoleModel(): string
    {
        return self::config('enterprise.models.enterprise_role', EnterpriseRole::class);
    }

    public static function config($key, $default = '')
    {
        $key = 'plugin.enterprise.' . $key;
        return config($key, $default);
    }

    public static function user(): ?EnterpriseUser
    {
        return self::guard()->user();
    }

    public static function guard()
    {
        return Auth::guard(self::config('enterprise.auth.guard') ?: 'enterprise');
    }

    public static function middleware(): array
    {
        var_dump('middleware');
        return [
            \plugin\owladmin\app\middleware\ConnectionDatabase::class,
            \plugin\owladmin\app\middleware\ForceHttps::class,
            \plugin\owladmin\app\middleware\AutoSetLocale::class,
            \plugin\owladmin\app\middleware\Authenticate::class,
            \plugin\owladmin\app\middleware\Permission::class,
        ];
    }
}